package home.my.base.sql;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ConditionQueryHunder{
	
	public static List<AbsCondition> buildConditionList(Object target){
		List<AbsCondition> conditionList = new ArrayList<AbsCondition>();
		String methodName = null;
		try {
			Method[] methods = target.getClass().getMethods();
			for (Method method : methods) {
				methodName = method.getName();
				Annotation[] annotations = method.getAnnotations();
				
				if (methodName.startsWith("get") && AnnotationConditionFactory.couldBuild(annotations)) {
					Object value =  method.invoke(target, new Object[]{});
					String column = null;
					if(methodName.startsWith("getSch"))
						column = methodName.substring(6, 7).toLowerCase()
					+ methodName.substring(7);
					
					for(Annotation annotation:annotations){
						AbsCondition condition =AnnotationConditionFactory.buildCondition(annotation,column, value);
						if(condition!=null)
							conditionList.add(condition);
					}
					
				}
			}
		} catch (Exception e) {
			System.out.println(methodName + "生成条件时报错 " + e.getMessage());
			e.printStackTrace();
		}
		return conditionList;
	}
}
